package org.cheniy.domain.strategy.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.cheniy.types.common.Constants;

import java.util.*;

/**
 * @author cheniy
 * @description Strategy需要的StrategyAward对象
 * @create 2025/3/1 23:37
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {


    /** 抽奖策略ID */
    private Long strategyId;
    /** 抽奖奖品ID【规则类型为策略，则不需要奖品ID】 */
    private Integer awardId;
    /** 抽象规则类型；1-策略规则、2-奖品规则 */
    private Integer ruleType;
    /** 抽奖规则类型【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】 */
    private String ruleModel;
    /** 抽奖规则比值 */
    private String ruleValue;
    /** 抽奖规则描述 */
    private String ruleDesc;


    // 充血方法

    /**
     * 查询策略规则中rule_weight的值，放入map中
     * @return Map<String, List<Integer>>
     */
    public Map<String, List<String>> getRuleWeightValues(){

        if (!"rule_weight".equals(ruleModel)) return null;
        // 分组（4000:102,103 5000:101,103）
        String[] ruleValueGroups = ruleValue.split(Constants.SPACE);

        HashMap<String, List<String>> ruleValueMap = new HashMap<>();
        for (String ruleValueGroup:ruleValueGroups){
            // 检查输入是否为空
            if (null==ruleValueGroup||ruleValueGroup.isEmpty()) return ruleValueMap;

            // 分离出范围作为key，奖品id列表作为值
            String[] ruleValueKeyAndValue = ruleValueGroup.split(Constants.COLON);
            if (ruleValueKeyAndValue.length!=2){
                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleValueGroup);
            }
            String[] ruleValues = ruleValueKeyAndValue[1].split(Constants.SPLIT);

            // 值生成列表
            ArrayList<String> ruleValueList = new ArrayList<>(Arrays.asList(ruleValues));

            ruleValueMap.put(ruleValueGroup,ruleValueList );

        }
        return ruleValueMap;
    }




}
